// https: // leetcode.cn/problems/fruit-into-baskets/submissions/560175190/

class Solution {
public:
    int totalFruit(vector<int>& a) {
        int now = 0, j = 0, mx = 0;
        map<int, int> cnt;
        for (int i = 0; i < a.size(); i++) {
            if (!cnt[a[i]]) now++;
            cnt[a[i]]++;
            while (now > 2) {
                cnt[a[j]]--;
                if (!cnt[a[j]]) now--;
                j++;
            }
            mx = max(mx, i - j + 1);
        }
        return mx;
    }
};